import requests
# 引入解析html包
from lxml import etree

# 斗罗大陆网 - 网址
url = 'https://qq.131439.xyz/book/douluodalu1/1.html'

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36',
}

# 请求
response = requests.get(url=url, headers=headers)
response.encoding = 'utf-8'
text = response.text
# print(body_text)
# 解析html
etree_text = etree.HTML(text)
# xpahth解析html
# 获取章节标题
body_text_title = etree_text.xpath('//div[@class="entry-tit"]/h1/text()')[0]
# 获取当前章节内容
body_text = etree_text.xpath('//div[@class="m-post"]/p/text()')
body_text = '\n'.join(body_text) # 将字符串、元组、列表中的元素以指定的字符(分隔符)连接生成一个新的字符串
# 获取下一章节链接
body_next_url = 'https://qq.131439.xyz' + etree_text.xpath('//table[@class="wenxue2"][1]//td[2]/a/@href')[0]
print(body_text_title)
print(body_text)
print(body_next_url)

# 章节下载本地
# fileName = '斗罗大陆-%s.txt' % (body_text_title)
# print(fileName)
# with open('斗罗大陆.txt', 'w', encoding='utf-8') as fp:
#     fp.write(body_text_title+'\n\n'+body_text)






'''
【实战】'  '.join(a) 实例：
>>#对序列进行操作（分别使用'  ' 、' - '与':'作为分隔符）
 >> a=['1','2','3','4','5']

>>print（ '  '.join(a)）
1 2 3 4 5

>>print（'.'.join(a)）
1.2.3.4.5
'''




